package com.xywl.escort.view.order;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.xywl.escort.R;
import com.xywl.escort.base.BaseActivity;
import com.xywl.escort.view.order.purchase.all.PurchaseOrderFragment;
import com.xywl.escort.view.order.purchase.completed.CompletedOrderFragment;
import com.xywl.escort.view.order.purchase.delivered.DeliveredOrderFragment;
import com.xywl.escort.view.order.purchase.payment.PendingPaymentOrderFragment;
import com.xywl.escort.view.order.purchase.receipt.ReceiptOrderFragment;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dagger.android.AndroidInjection;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.support.HasSupportFragmentInjector;

/**
 * 采购订单
 */

@Route(path = "/xy/order/purchase")
public class PurchaseOrderActivity extends BaseActivity implements HasSupportFragmentInjector {

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.img_btn_title_right)
    ImageButton mImgBtnTitleRight;

    @Inject
    DispatchingAndroidInjector<Fragment> supportFragmentInjector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase_order);
        ButterKnife.bind(this);

        showBackButton();
        setTitleText(R.string.purchase_order);

        mImgBtnTitleRight.setVisibility(View.VISIBLE);
        mImgBtnTitleRight.setImageResource(R.mipmap.kits_procurement_search);

        Fragment allFragment = PurchaseOrderFragment.newInstance(-1);
        Fragment unpaidFragment = PendingPaymentOrderFragment.newInstance(0);
        Fragment undeliveredFragment = DeliveredOrderFragment.newInstance(2);
        Fragment unreceivedFragment = ReceiptOrderFragment.newInstance(3);
        Fragment completedFragment = CompletedOrderFragment.newInstance(4);
        String[] tabNames = new String[]{"全部", "待付款", "待发货", "待收货", "已完成"};
        Fragment[] fragments = new Fragment[]{allFragment, unpaidFragment, undeliveredFragment, unreceivedFragment, completedFragment};
        PurchasePagerAdapter adapter = new PurchasePagerAdapter(getSupportFragmentManager(), tabNames, fragments);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(tabNames.length);
        mViewPager.setCurrentItem(0);
    }

    @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return supportFragmentInjector;
    }

    @OnClick(R.id.img_btn_title_right)
    public void onViewClicked() {
        ARouter.getInstance().build("/xy/order/search").navigation();
    }

    class PurchasePagerAdapter extends FragmentPagerAdapter {
        private String[] tabNames;
        private Fragment[] mFragments;

        private PurchasePagerAdapter(FragmentManager fm, String[] tabNames, Fragment[] fragments) {
            super(fm);
            this.tabNames = tabNames;
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments[position];
        }

        @Override
        public int getCount() {
            return tabNames.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabNames[position];
        }
    }
}
